/**
 * @param {number[]} nums1
 * @param {number} m
 * @param {number[]} nums2
 * @param {number} n
 * @see https://leetcode-cn.com/problems/merge-sorted-array/solution/he-bing-liang-ge-you-xu-shu-zu-by-leetcode/
 * @description 从前往后遍历，需要空间存放。从后往前遍历，直接修改nums1数组即可，因为有0占位
 * @return {void} Do not return anything, modify nums1 in-place instead.
 */
var merge = function(nums1, m, nums2, n) {
  // 指针最后
  let i = m - 1;
  let j = n - 1;
  let p = m + n - 1;
  while(i >= 0 || j >= 0) {
    let A = nums1[i];
    let B = nums2[j];
    if(i === -1) {
      nums1[p] = nums2[j];
      j--;
    } else if(j === -1) {
      nums1[p] = nums1[i];
      i--;
    } else if(A < B) {
      nums1[p] = B;
      j--;
    } else {
      nums1[p] = A;
      i--;
    }
    p--;
  }
  console.log(nums1)
};

merge([1,2,3,0,0,0], 3, [2,5,6], 3);

function getLeftItems(arr, startIndex, nums) {
  let leftArr = [];
  for (; startIndex < nums; startIndex++) {
    leftArr.push(arr[startIndex]);
  }
  return leftArr;
}

function getElement(arr, index) {
  return arr[index];
}